package com.gfeeling.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gfeeling.model.Menu;
import com.gfeeling.service.MenuService;
import com.gfeeling.utils.UUIDGenerator;

/**
 * 菜单控制类
 * 
 * @author lsl
 * @date 2016年7月18日
 * @comment
 * @version 1.0
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public @ResponseBody
	List<Menu> list() {
		List<Menu> list = null;
		logger.info("");
		return list;
	}

	/**
	 * 添加菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	public @ResponseBody
	Map<String, Object> save(Menu menu) {
		logger.info("add menu:[name={},url={}]", menu.getName(), menu.getUrl());
		boolean isModify = false;
		String id = menu.getId();
		// 判断是修改还是添加
		if (StringUtils.hasText(id)) {
			isModify = true;
		}
		menu.setType(1);// 菜单类型默认为1
		// 修改
		if (isModify) {
			try {
				this.menuService.saveOrUpdate(menu);
				returnSuccess();
			} catch (Exception e) {
				returnFail("服务器异常");
				logger.error("save menu:-->", e.getMessage());
			}
		} else {
			// 添加
			menu.setId(UUIDGenerator.getUUID());
			returnSuccess();
			try {
				this.menuService.save(menu);
			} catch (Exception e) {
				logger.error("save menu:-->", e.getMessage());
				returnFail("服务器异常");
			}
		}
		return map;

		/*
		 * List<Resource> resourceList = new ArrayList<Resource>() ;
		 * if(menu.getResource() != null&&StringUtils.hasText(menu.getKey()) ){
		 * for(String r : menu.getResource()){ String[] ss =
		 * r.split(Constants.RESOURCE_DELIMITER) ; if(ss.length == 2){ Resource
		 * resource = new Resource() ; resource.setMenuId(menu.getId());
		 * resource.setName(ss[1]); resource.setKey(menu.getKey() +
		 * Constants.RESOURCE_DELIMITER + ss[0]);
		 * resource.setSort(resourceService.getResourceTypeSort(ss[0]));
		 * resourceList.add(resource) ; } } }
		 * this.resourceService.saveAll(resourceList, menu.getId());
		 */
	}

}
